package cn.nfwx.tv.adapter;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;

import cn.nfwx.tv.R;

/**
 * Created by zhouzechao on 2017/11/17.
 */

public class LookBackDateAdapter2 extends BaseQuickAdapter<String, BaseViewHolder> implements LifecycleObserver {
    private int mSelect = -1;//选中项

    public LookBackDateAdapter2() {
        super(R.layout.activity_tvprogram_left_item, Arrays.asList("08/29", "08/30", "昨天", "今天", "09/02", "09/03", "09/04", "09/05"));
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.add(Calendar.DAY_OF_MONTH, -1);
        int preYear = calendar.get(Calendar.YEAR);
        int premonth = calendar.get(Calendar.MONTH);
        int preday = calendar.get(Calendar.DAY_OF_MONTH);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            calendar.setTime(dateFormat.parse(item));
            int cYear = calendar.get(Calendar.YEAR);
            int cmonth = calendar.get(Calendar.MONTH);
            int cday = calendar.get(Calendar.DAY_OF_MONTH);

            if (cYear == year && cmonth == month && cday == day) {
                helper.setText(R.id.tv_title, "今天");
            } else if (cYear == preYear && cmonth == premonth && cday == preday) {
                helper.setText(R.id.tv_title, "昨天");
            } else {
                dateFormat = new SimpleDateFormat("MM/dd");
                helper.setText(R.id.tv_title, dateFormat.format(calendar.getTime()));
            }
            if (mSelect == helper.getLayoutPosition()) {
                helper.getView(R.id.tv_title).setSelected(true);
            } else {
                helper.getView(R.id.tv_title).setSelected(false);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void setSelect(int position) {
        mSelect = position;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void stop() {
        setNewData(new ArrayList<String>());
    }
}
